package com.mrobuy.jdvc.dao.impl;

import com.gavel.database.SQLExecutor;
import com.gavel.utils.StringUtils;
import com.mrobuy.jdvc.entity.VCProduct;
import com.mrobuy.util.Pagination;
import com.mrobuy.jdvc.dao.VCBrandDao;
import com.mrobuy.jdvc.dao.VCProductApplyDao;
import com.mrobuy.jdvc.entity.VCProductApply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Repository("vcProductApplyDao")
public class VCProductApplyDaoImpl extends CommonDaoImpl implements VCProductApplyDao {

    private static Logger LOG = LoggerFactory.getLogger(VCBrandDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<VCProductApply> list() {

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from vc_product_apply");
        try {
            List<VCProductApply> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCProductApply.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Pagination load(int start, int length, VCProductApply condition) {
        Pagination pagination = new Pagination();

        List<Object> params = new ArrayList<>();

        StringBuilder whereCond = new StringBuilder();
        if ( condition.getState()!=null ) {
            whereCond.append(" and state = ? ");
            params.add(condition.getState());
        }

        if ( condition.getReject()!=null ) {
            whereCond.append(" and reject = ? ");
            params.add(condition.getReject());
        }

        try {

            int count = jdbcTemplate.queryForObject("select count(*) from vc_product_apply where ZT = 1 " + whereCond.toString(), params.toArray(new Object[params.size()]), Integer.class);

            params.add(start);
            params.add(length);
            List<VCProductApply> list = jdbcTemplate.query("select * from vc_product_apply where  ZT = 1 " + whereCond.toString() + " limit ?, ?", params.toArray(new Object[params.size()]), new BeanPropertyRowMapper(VCProductApply.class));


            pagination.setRecordsTotal(count);
            pagination.setRecordsFiltered(count);
            pagination.setData(list);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return pagination;
    }

    @Override
    public void insert(VCProductApply productApply) {

        if ( productApply==null ) {
            return ;
        }

        try {
            SQLExecutor.tempLateInsert(productApply, jdbcTemplate);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void update(VCProductApply productApply) {
        if ( productApply==null ) {
            return ;
        }

        try {
            SQLExecutor.tempLateUpate(productApply, jdbcTemplate, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public VCProductApply load(String applyId) {
        if (StringUtils.isBlank(applyId)) {
            return null;
        }

        try {
            return (VCProductApply)jdbcTemplate.queryForObject("select * from VC_PRODUCT_APPLY where APPLYID = ?", new Object[]{applyId}, new BeanPropertyRowMapper(VCProductApply.class));
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }

    @Override
    public List<VCProductApply> loadApplies(String vendor) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from vc_product_apply where VENDOR = ? ");
        try {
            List<VCProductApply> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCProductApply.class), vendor);
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public List<VCProductApply> loadAppliesWithoutComplete(String vendor) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from vc_product_apply where STATE <> 306 and VENDOR = ? ");
        try {
            List<VCProductApply> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(VCProductApply.class), vendor);
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }
}
